import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import jwt from 'jsonwebtoken'
import moment from 'moment'
const Login = () =>
  import(/* webpackChunkName: 'login' */ '../views/Login.vue')
const Reg = () => import(/* webpackChunkName: 'reg' */ '../views/Reg.vue')
const Forget = () =>
  import(/* webpackChunkName: 'forget' */ '../views/Forget.vue')
const Home = () => import(/* webpackChunkName: 'home' */ '../views/Home.vue')
const Index = () =>
  import(/* webpackChunkName: 'index' */ '../views/channels/Index.vue')
const Template1 = () =>
  import(/* webpackChunkName: 'template1' */ '../views/channels/Template1.vue')
const Center = () =>
  import(/* webpackChunkName: 'center' */ '../views/Center.vue')
const UserCenter = () =>
  import(/* webpackChunkName: 'user-center' */ '@/components/user/Center.vue')
const UserMsg = () =>
  import(/* webpackChunkName: 'user-msg' */ '@/components/user/Msg.vue')
const UserOthers = () =>
  import(/* webpackChunkName: 'user-others' */ '@/components/user/Others.vue')
const UserPosts = () =>
  import(/* webpackChunkName: 'user-posts' */ '@/components/user/Posts.vue')
const PostMy = () =>
  import(
    /* webpackChunkName: 'post-my' */ '@/components/user/common/MyPost.vue'
  )
const PostCollection = () =>
  import(
    /* webpackChunkName: 'post-collection' */ '@/components/user/common/MyCollection.vue'
  )
const UserSettings = () =>
  import(
    /* webpackChunkName: 'user-settings' */ '@/components/user/Settings.vue'
  )
const SetInfo = () =>
  import(
    /* webpackChunkName: 'set-info' */ '@/components/user/common/MyInfo.vue'
  )
const SetPic = () =>
  import(
    /* webpackChunkName: 'set-info' */ '@/components/user/common/PicUpload.vue'
  )
const SetPass = () =>
  import(
    /* webpackChunkName: 'set-info' */ '@/components/user/common/Passwd.vue'
  )
const SetAccount = () =>
  import(
    /* webpackChunkName: 'set-info' */ '@/components/user/common/Accounts.vue'
  )
const User = () => import(/* webpackChunkName: 'user' */ '../views/User.vue')
const NotFound = () => import(/* webpackChunkName: 'not-found' */ '../views/NotFound.vue')
const Confirm = () => import(/* webpackChunkName: 'confirm' */ '../views/Confirm.vue')
const Reset = () => import(/* webpackChunkName: 'reset' */ '../views/Reset.vue')
const Add = () => import(/* webpackChunkName: 'add' */ '@/components/contents/Add.vue')
const Edit = () => import(/* webpackChunkName: 'edit' */ '@/components/contents/Edit.vue')
const Detail = () => import(/* webpackChunkName: 'detail' */ '@/components/contents/Detail.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Home,
    children: [
      {
        path: '',
        name: 'index',
        component: Index
      },
      {
        path: '/index/:catalog',
        name: 'catalog',
        component: Template1
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  }, {
    path: '/confirm',
    name: 'confirm',
    component: Confirm
  }, {
    path: '/reset',
    name: 'reset',
    component: Reset
  },
  {
    path: '/reg',
    name: 'reg',
    component: Reg,
    beforeEnter: (to, from, next) => {
      if (from.name !== 'login') {
        next('/login')
      } else {
        next()
      }
    }
  },
  {
    path: '/forget',
    name: 'forget',
    component: Forget
  },
  {
    path: '/add',
    name: 'add',
    meta: { requiredAuth: true },
    component: Add
  },
  {
    path: '/edit/:tid',
    props: true,
    name: 'edit',
    meta: { requiredAuth: true },
    component: Edit,
    beforeEnter: (to, from, next) => {
      if (['detail', 'mypost'].indexOf(from.name) !== -1 && to.params.page && to.params.page.isEnd === 0) {
        next()
      } else {
        let editData = localStorage.getItem('editData')
        if (editData) {
          editData = JSON.parse(editData)
          if (editData.isEnd === 0) {
            next()
          } else {
            next('/')
          }
        } else {
          next('/')
        }
      }
    }
  },
  {
    path: '/detail/:tid',
    name: 'detail',
    props: true,
    meta: {
      type: ['get', 'edit', 'delete']
    },
    component: Detail
  },
  {
    path: '/user/:uid',
    props: true,
    name: 'home',
    component: User
  },
  {
    path: '/center',
    meta: { requiredAuth: true },
    component: Center,
    children: [
      {
        path: '/',
        name: 'center',
        component: UserCenter
      },
      {
        path: '/msg',
        name: 'msg',
        component: UserMsg
      },
      {
        path: '/posts',
        name: '',
        component: UserPosts,
        children: [
          {
            path: '/',
            name: 'mypost',
            component: PostMy
          },
          {
            path: 'collection',
            name: 'mycollection',
            component: PostCollection
          }
        ]
      },
      {
        path: '/others',
        name: 'others',
        component: UserOthers
      },
      {
        path: '/set',
        name: '',
        component: UserSettings,
        children: [
          {
            path: '/',
            name: 'info',
            component: SetInfo
          },
          {
            path: 'pic',
            name: 'pic',
            component: SetPic
          },
          {
            path: 'pass',
            name: 'passwd',
            component: SetPass
          },
          {
            path: 'account',
            name: 'account',
            component: SetAccount
          }
        ]
      }
    ]
  },
  {
    path: '/404',
    name: '404',
    component: NotFound
  },
  {
    path: '*',
    redirect: '/404'
  }
]

const router = new VueRouter({
  linkExactActiveClass: 'layui-this',
  routes
})
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (token) {
    const data = jwt.decode(token)
    if (data && data.exp && moment().isBefore(moment(data.exp * 1000))) {
      store.commit('setToken', token)
      store.commit('setUserInfo', JSON.parse(localStorage.getItem('userInfo')))
      store.commit('setLoginStatus', true)
      // websocket初始化
      if (!store.state.ws) {
        store.commit('initWebSocket', {})
      }
    } else {
      localStorage.clear()
    }
  }
  if (to.matched.some(item => item.meta.requiredAuth)) {
    const isLogin = store.state.isLogin
    if (isLogin) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
